package com.emar.adx.yiqifa.util;

import org.apache.commons.lang3.StringUtils;

import java.io.ByteArrayOutputStream;
import java.math.RoundingMode;
import java.text.DecimalFormat;
import java.util.Date;

/**
 * Created by xiaoliang on 2015/3/8 15:38.
 */
public class FunctionUtil {
    /**
     * the following const is to define date format.
     */
    public static final int FMT_DATE_YYYYMMDD = 1;
    public static final int FMT_DATE_YYYYMMDD_HHMMSS = 2;
    public static final int FMT_DATE_HHMMSS = 3;
    public static final int FMT_DATE_HHMM = 4;

    public static final String UTF_8 = "UTF-8";
    public static final String GBK = "GBK";

    public static String parseDefaultValue(String str){
        if(str == null || "".equals(str.trim())){
            return "";
        }else
            return str;
    }

    public static Integer parseDefaultValue(Integer i){
        if(i==null){
            return 0;
        }else {
            return i;
        }
    }

    public static Long parseDefaultValue(Long i){
        if(i==null){
            return (long)0;
        }else {
            return i;
        }
    }

    public static Double parseDefaultValue(Double d){
        if(d == null){
            return (double)0;
        }else
            return d;
    }
    /**
     * @param data
     * @return
     */
    public static Double formatDouble(Double data){
        DecimalFormat d = new DecimalFormat(".00");
        d.setRoundingMode(RoundingMode.DOWN);
        String format = d.format(data);
        return Double.parseDouble(format);
    }
    /**
     * @param data
     * @return
     */
    public static Double formatDoubleByHalfUp(Double data){
        DecimalFormat d = new DecimalFormat(".00");
        d.setRoundingMode(RoundingMode.HALF_UP);
        String format = d.format(data);
        return Double.parseDouble(format);
    }

    public static Date parseDefaultValue(Date date){
        if(date == null){
            return new Date();
        }else
            return date;
    }

    /**
     * @param strDate
     * @param nFmtDate
     * @return
     */
    public static Date parseDate(String strDate, int nFmtDate)// throws Exception
    {

        java.text.SimpleDateFormat fmtDate = null;
        java.text.ParsePosition pos = new java.text.ParsePosition(0);
        switch (nFmtDate)
        {
            default:
            case FMT_DATE_YYYYMMDD:
                fmtDate = new java.text.SimpleDateFormat("yyyy-MM-dd");
                break;
            case FMT_DATE_YYYYMMDD_HHMMSS:
                fmtDate = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                break;
            case FMT_DATE_HHMM:
                fmtDate = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                break;
            case FMT_DATE_HHMMSS:
                fmtDate.applyLocalizedPattern("HH:mm:ss");
                break;
        }
//        return fmtDate.parse(strDate, pos);
        Date dtRet=null;
        try
        {
            return     dtRet=fmtDate.parse(strDate, pos);
        }
        catch(Exception e)
        {

        }
        return dtRet;

    }

    /**
     * @param s
     * @param defaultValue
     * @return
     */
    public static int parseInt(String s, int defaultValue)
    {
        if (s == null || "".equals(s))
        {
            return defaultValue;
        }

        int nValue = defaultValue;
        try
        {
            nValue = Integer.parseInt(s);
        }
        catch (Exception e)
        {
        }
        return nValue;
    }

    /**
     * @param s
     * @param defaultValue
     * @return
     */
    public static long parseLong(String s, long defaultValue)
    {
        if (s == null || "".equals(s))
        {
            return defaultValue;
        }

        long nValue = defaultValue;
        try
        {
            nValue = Long.parseLong(s.trim());
        }
        catch (Exception e)
        {
        }
        return nValue;
    }

    /**
     * @param s
     * @param defaultValue
     * @return
     */
    public static double parseDouble(String s, double defaultValue)
    {
        if (s == null || "".equals(s))
        {
            return defaultValue;
        }

        double dblValue = defaultValue;
        try
        {
            dblValue = Double.parseDouble(s);
        }
        catch (Exception e)
        {
        }
        return dblValue;
    }

    /**
     * @param s
     * @param defaultValue
     * @return
     */
    public static float parseFloat(String s, float defaultValue)
    {
        if (s == null || "".equals(s))
        {
            return defaultValue;
        }
        float fValue = defaultValue;
        try
        {
            fValue = Float.parseFloat(s);
        }
        catch (Exception e)
        {
        }
        return fValue;
    }

    public static String parseString(String s,String defaultValule){
        if(StringUtils.isBlank(s)){
            return defaultValule;
        }
        return s;
    }

    public static String parseDateToString(Date date, int nFmtDate)
    {
        String back = "";
        if (date == null || "".equals(date.toString()))
        {
            return back;
        }

        java.text.SimpleDateFormat fmtDate = null;
        java.text.ParsePosition pos = new java.text.ParsePosition(0);
        switch (nFmtDate)
        {
            default:
            case FMT_DATE_YYYYMMDD:
                fmtDate = new java.text.SimpleDateFormat("yyyy-MM-dd");
                break;
            case FMT_DATE_YYYYMMDD_HHMMSS:
                fmtDate = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                break;
            case FMT_DATE_HHMM:
                fmtDate = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                break;
            case FMT_DATE_HHMMSS:
                fmtDate.applyLocalizedPattern("HH:mm:ss");
                break;
        }

        try
        {
            back = fmtDate.format(date);
        }
        catch (Exception e)
        {
        }
        return back;
    }


    /**
     */
    public static int length(String s) {
        if (s == null)
            return 0;
        char[] c = s.toCharArray();
        int len = 0;
        for (int i = 0; i < c.length; i++) {
            len++;
            if (!isLetter(c[i])) {
                len++;
            }
        }
        return len;
    }

    /**
     */
    public static boolean isLetter(char c) {
        int k = 0x80;
        return c / k == 0 ? true : false;
    }

    public static String toHexToStringUtf8(String s)
    {
        byte[] baKeyword = new byte[s.length()/2];
        for(int i = 0; i < baKeyword.length; i++)
        {
            try
            {
                baKeyword[i] = (byte)(0xff & Integer.parseInt(s.substring(i*2, i*2+2),16));
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        }
        try
        {
            s = new String(baKeyword, "utf-8");
        }
        catch (Exception e1)
        {
            e1.printStackTrace();
        }
        return s;
    }

    private static String hexString="0123456789ABCDEF";

    public static String stringToHex(String str)
    {
        byte[] bytes=str.getBytes();
        StringBuilder sb=new StringBuilder(bytes.length*2);
        for(int i=0;i<bytes.length;i++)
        {
            sb.append(hexString.charAt((bytes[i]&0xf0)>>4));
            sb.append(hexString.charAt((bytes[i]&0x0f)>>0));
        }
        return sb.toString();
    }
    public static String hexToString(String bytes)
    {
        ByteArrayOutputStream baos=new ByteArrayOutputStream(bytes.length()/2);
        for(int i=0;i<bytes.length();i+=2)
            baos.write((hexString.indexOf(bytes.charAt(i))<<4 |hexString.indexOf(bytes.charAt(i+1))));
        return new String(baos.toByteArray());
    }
}
